package org.example.word;

import com.alibaba.fastjson.JSONObject;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.*;

import java.io.*;
import java.util.*;
import java.util.stream.Stream;

public class ReadWord97 {
    //public static final String FILE_PATH = "E:\\New folder\\来访信息管理--政务已办模板.doc";
    public static final String FILE_PATH = "E:\\New folder\\已办办件.doc";

    //public static final String FILE_PATH = "E:\\New folder\\1.doc";
    public static void main(String[] args) {
        try (InputStream inputStream = new FileInputStream(new File(FILE_PATH))) {
            try (HWPFDocument document = new HWPFDocument(inputStream)) {
                Range range = document.getRange();
                TableIterator iterator = new TableIterator(range);
                while (iterator.hasNext()) {
                    Table table = iterator.next();
                    /*String title = table.getRow(1).getCell(1).text();
                    System.err.println("办件标题：" + StringTrim(title));
                    String content = table.getRow(2).getCell(1).text();
                    System.err.println("内容：" + StringTrim(content));
                    String name = table.getRow(3).getCell(1).text();
                    System.err.println("姓名：" + StringTrim(name));
                    String telephone = table.getRow(4).getCell(1).text();
                    System.err.println("固定电话：" + StringTrim(telephone));
                    String mobile = table.getRow(5).getCell(1).text();
                    System.err.println("手机号码：" + StringTrim(mobile));
                    String address = table.getRow(6).getCell(1).text();
                    System.err.println("地址：" + StringTrim(address));
                    String email = table.getRow(7).getCell(1).text();
                    System.err.println("E_Mail：" + StringTrim(email));
                    String postCode = table.getRow(8).getCell(1).text();
                    System.err.println("邮政编码：" + StringTrim(postCode));
                    String idType = table.getRow(9).getCell(1).text();
                    System.err.println("证件类型：" + StringTrim(idType));
                    String idNumber = table.getRow(10).getCell(1).text();
                    System.err.println("证件号码：" + StringTrim(idNumber));
                    String sex = table.getRow(11).getCell(1).text();
                    System.err.println("性别：" + StringTrim(sex));
                    String age = table.getRow(12).getCell(1).text();
                    System.err.println("年龄：" + StringTrim(age));

                    String attachmentFile = table.getRow(13).getCell(1).text();
                    System.err.println("附件：" + attachmentFile);

                    String category = table.getRow(14).getCell(1).text();
                    System.err.println("办件分类：" + StringTrim(category));

                    String target = table.getRow(15).getCell(1).text();
                    System.err.println("提交对象：" + StringTrim(target));
                    String isPublic = table.getRow(15).getCell(3).text();
                    System.err.println("是否愿意公开：" + StringTrim(isPublic));

                    String submitTime = table.getRow(16).getCell(1).text();
                    System.err.println("提交事件：" + StringTrim(submitTime));
                    String workNum = table.getRow(16).getCell(3).text();
                    System.err.println("办件编号：" + StringTrim(workNum));
                    String ip = table.getRow(17).getCell(1).text();
                    System.err.println("IP:" + StringTrim(ip));

                    String replyContent = getParagraphText(table.getRow(20).getCell(1));
                    System.err.println("回复内容：" + StringTrim(replyContent));

                    String replyUnit = table.getRow(21).getCell(1).text();
                    System.err.println("答复机构：" + StringTrim(replyUnit));
                    String replyTime = table.getRow(21).getCell(3).text();
                    System.err.println("答复时间：" + StringTrim(replyTime));

                    String replyAttach = table.getRow(22).getCell(1).text();
                    System.err.println("答复附件：" + StringTrim(replyAttach));
                    int numRows = table.numRows();
                    System.err.println("numRows:" + numRows);*/

                    getHyperlink(table.getRow(13).getCell(1));

                }

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 去空格
     *
     * @param str
     * @return
     */
    private static String StringTrim(String str) {
        return str.replaceAll("[\\s\\u00A0]+", "").trim();
    }

    private static String getParagraphText(TableCell cell) {
        StringBuilder sBuilder = new StringBuilder();
        int numParagraph = cell.numParagraphs();
        for (int i = 0; i < numParagraph; i++) {
            String text = cell.getParagraph(i).text();
            System.err.println("text=" + text);
            sBuilder.append(text);
        }
        return sBuilder.toString();

    }

    private static List<Map<String, Object>> getHyperlink(TableCell cell) {
        List<Map<String, Object>> maps = new ArrayList<>();
        String cellText = StringTrim(cell.text()).trim();
        if (StringUtils.isNotEmpty(cellText) && cellText.contains("HYPERLINK")) {
            int numParagraphs = cell.numParagraphs();
            for (int i = 0; i < numParagraphs; i++) {
                String text = cell.getParagraph(i).text();
                String[] strArray = text.split("点击下载");
                for (String s : strArray) {
                    String str = StringTrim(s).trim();
                    if (StringUtils.isNotEmpty(str)) {
                        String[] fileArrays = str.split("HYPERLINK");
                        String fileName = StringTrim(fileArrays[0]);
                        String fileUrl = fileArrays[1].replace("\"", "");
                        Map<String, Object> map = new HashMap<>();
                        map.put("fileName", fileName);
                        map.put("fileUrl", fileUrl);
                        maps.add(map);
                    }
                }
            }
        }

        System.err.println(JSONObject.toJSONString(maps, true));
        return maps;
    }
}
